package com.hongxun.budget.ui.dialog;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.tk.SetMainActivity;
import com.hongxun.budget.ui.adapter.SpinnerOffBgAdapter;
import com.hongxun.budget.utils.DBUtils;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.entity.PointItem;
import com.hongxun.greenentity.tk.APBaseEntity;
import com.hongxun.greenentity.tk.DPointEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;
import com.yuyh.library.imgsel.widget.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 共用listDialog和listcheckDialog的页面，如需修改该页面请重新新建新的页面并指定。
 */

public class PointChoiseDialog extends BaseCenterFragmentDialog {

    @BindView(R.id.title)
    TextView titleTv;
    @BindView(R.id.list)
    RecyclerView list;
    String title;
    List<PointItem> datas = new ArrayList<>();
    SuperAdapter<PointItem> adapter;
    @BindView(R.id.spinner)
    Spinner spinner;
    SpinnerOffBgAdapter<String> spinnerOffBgAdapter;
    String[] deviceArr;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_point_choice;
    }

    @Override
    protected void initViews() {
        String guid = (String) SPUtils.get(context, "project_guid", "");
        deviceArr = getResources().getStringArray(R.array.process_mode);
        spinner.setAdapter(spinnerOffBgAdapter = new SpinnerOffBgAdapter<String>(context, Arrays.asList(deviceArr), spinner));
        APBaseEntity apBaseEntity = GreenDaoManager.getInstance().getmDaoSession().getAPBaseEntityDao().load(guid);
        if (apBaseEntity.getPBType().equals("100") || apBaseEntity.getPBType().equals("200")) {//架空线路
            datas.addAll(PointItem.getJKPointItem());
        } else {
            datas.addAll(PointItem.getSBPointItem());
        }
        if (!TextUtils.isEmpty(title)) titleTv.setText(title);
        list.setLayoutManager(new GridLayoutManager(context, 3));
        list.addItemDecoration(new DividerGridItemDecoration(context));
        list.setAdapter(adapter = new SuperAdapter<PointItem>(R.layout.content_dialog_point) {
            @Override
            public void bindView(ViewHolder viewHolder, final PointItem pointItem, int i) {
                Glide.with(getActivity()).load(pointItem.getUri()).into((ImageView) viewHolder.getView(R.id.image_top));
                ((TextView) viewHolder.getView(R.id.text)).setText(pointItem.getName());
                viewHolder.getView(R.id.layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        SPUtils.put(context, "process", spinnerOffBgAdapter.getSelectedItem());
                        SPUtils.put(context, "point_name", pointItem.getName());
                        startActivity(new Intent(context, SetMainActivity.class));
                        dismiss();
                    }
                });
            }
        });
        adapter.setData(datas);
    }

    @Override
    public void initDefault() {
        DPointEntity lastPoint = DBUtils.getLastPoint(context);
        String process = lastPoint == null ? "" : lastPoint.getPPWorktype();
        if (!process.equals(""))//设备最后一次的操作类型为默认操作类型
            for (int i = 0; i < deviceArr.length; i++) {
                if (process.equals(deviceArr[i])) {
                    spinner.setSelection(i);
                }
            }
    }

    public PointChoiseDialog setTitle(String title) {
        this.title = title;
        if (titleTv != null) titleTv.setText(title);
        return this;
    }

    @OnClick(R.id.cancel)
    public void onViewClicked() {
        dismiss();
    }

}
